import { http } from "@/utils/http";

export type UserResult = {
  success: boolean;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** 按钮级别权限 */
    permissions: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getLogin = (vcodeid: string, data?: object) => {
  return http.request<UserResult>("post", "/lt2_user/login", { data, headers: { "Vcodeid": vcodeid } });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", { data });
};

interface VcodeResponse {
  code: number;
  vcode: {
    vcode_value: string;
  };
}

export const getVcode = (vcodeid?: string) => {
  return http.request<VcodeResponse>("post", "/lt2_user/getVcode", { data: {}, headers: { "Vcodeid": vcodeid } });
};

export const apiListUserMid = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/listUserMid", { data });
};

export const apiListClient = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/listClient", { data });
};

export const apiListUser = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/listUser", { data });
};

export const apiAddUser = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/addUser", { data });
};

export const apiEditUser = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/editUser", { data });
};

export const apiEditPasswd = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/editPasswd", { data });
};

export const apiGetUser = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/getUser", { data });
};

export const apiDelUser = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/delUser", { data });
};

export const apiEditUserMid = (data?: Object) => {
  return http.request<Object>("post", "/lt2_user/editUserMid", { data });
};